package parkshop.pmcourse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import parkshop.pmcourse.domain.Advertisement;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.service.AdvertisementService;
import parkshop.pmcourse.utils.ConstantUtil;

@Controller
@RequestMapping("/advertisement")
public class AdvertisementCtrl {

	@Autowired
	private AdvertisementService advertisementService;
	
	@PostMapping("/upload")
	public String uploadAdvertisement(Advertisement advertisement,HttpServletRequest request,  Model model){
		User curUser=(User) request.getSession().getAttribute(ConstantUtil.nameOfCurUser);
		advertisementService.saveAdvertisement(advertisement, curUser,request);
		return "redirect:/advertisement/list/1";
	}
	@GetMapping("/upload")
	public String goUploadAdvertisement(HttpSession httpSession,  Model model){

		model.addAttribute("isAdvertisement", "advertisement");
		return "shopOwner/productUpload";
	}
	
	@GetMapping("/list/{pageNumber}")
	public String listAdvertisement(@PathVariable int pageNumber,HttpSession httpSession,  Model model){

		User user=(User) httpSession.getAttribute(ConstantUtil.nameOfCurUser);
		PageBean pageBean=advertisementService.getPageAdvertisementsByUserId(user.getType(),user.getId(),pageNumber);
		model.addAttribute("pageBean", pageBean);
		
		return "shopOwner/advertisementManage";
	}
	@GetMapping("/delete/{id}")
	public String deleteAdvertisement(@PathVariable int id,HttpSession httpSession,  Model model){
		
		User user=(User) httpSession.getAttribute(ConstantUtil.nameOfCurUser);
		advertisementService.deleteAdvertisements((long) id,user);
		return "redirect:/advertisement/list/1";
	}
	@GetMapping("/update/{id}")
	public String updateAdvertisement(@PathVariable int id,HttpServletRequest request,  Model model){
		
		Advertisement advertisement=advertisementService.getAdvertisementByADId((long) id);
		
		if(!advertisement.getUrl().startsWith("http")){
		String localHost="http://"+request.getServerName()+":"+request.getServerPort();
		advertisement.setUrl(localHost+advertisement.getUrl());
		}
		
		model.addAttribute("advertisement", advertisement);
		model.addAttribute("isAdvertisement", "advertisement");
		return "shopOwner/productUpload";
	}
	@PostMapping("/update")
	public String doUpdateAdvertisement(Advertisement advertisement,HttpServletRequest request,  Model model){
		
		advertisementService.updateAdvertisements(advertisement,request);
		
		List<Advertisement> list=new ArrayList<Advertisement>();
		list.add(advertisement);
		PageBean pageBean=new PageBean();
		pageBean.setRecordList(list);
		pageBean.setCurrentPage(1);
		pageBean.setPageCount(1);
		pageBean.setRecordCount(1);
        model.addAttribute("pageBean", pageBean);		
		return "shopOwner/advertisementManage";
	}
	
}
